import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;


public class PrototypeGui {
	public static JFrame frame;
	public static Console console;
	public static SidePanel side;
	public static DisplayPanel disp;
	
	public static void main(String[] args){
		frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
		
		JPanel panel = new JPanel(new BorderLayout());
		JPanel centerPanel = new JPanel(new GridBagLayout());
		
		panel.add(centerPanel, BorderLayout.CENTER);
		FileMenu menu = new FileMenu();
		panel.add(menu, BorderLayout.NORTH);
		
		//constructing side panel
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.gridheight = 6;
		gbc.gridwidth = 3;
		gbc.weightx = .25;
		gbc.weighty = 1;
		gbc.fill = GridBagConstraints.BOTH;
		side = new SidePanel();
		JScrollPane sideScroll = new JScrollPane(side);
		centerPanel.add(sideScroll, gbc);
		
		gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 6;
		gbc.gridheight = 2;
		gbc.gridwidth = 10;
		gbc.weightx = 1;
		gbc.weighty = .25;
		gbc.fill = GridBagConstraints.BOTH;
		console = new Console();
		JScrollPane scroll = new JScrollPane(console);
		centerPanel.add(scroll, gbc);
		
		JPanel saveAsPanel = new JPanel(new GridBagLayout());
		JPanel space2 = new JPanel();
		gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.weightx = 1;
		gbc.weighty = 0;
		saveAsPanel.add(space2, gbc);
		JButton saveAs = new JButton("Save As");
		gbc = new GridBagConstraints();
		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.weightx = 0;
		gbc.weighty = 0;
		saveAsPanel.add(saveAs, gbc);
		gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 11;
		gbc.gridwidth = 10;
		gbc.insets = new Insets(5, 5, 5, 5);
		gbc.weightx = 1;
		gbc.weighty = 0;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		centerPanel.add(saveAsPanel, gbc);
		
		//construction of the display panel
		gbc = new GridBagConstraints();
		gbc.gridx = 3;
		gbc.gridy = 0;
		gbc.gridheight = 6;
		gbc.gridwidth = 7;
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.fill = GridBagConstraints.BOTH;
		disp = new DisplayPanel();
		centerPanel.add(disp, gbc);
		
		frame.add(panel);
		frame.setVisible(true);
		
		//looking for dot preference
		File preferences = new File("c:\\Users\\Jamison\\Documents\\SE416\\Preferences.xml");
		String dot = getDotLocation(preferences);
		if (dot == null){
			JOptionPane.showMessageDialog(panel, "The location of your dot was not detected please select its location");
			JFileChooser fc = new JFileChooser();
			int val = fc.showOpenDialog(panel);
			if (val == JFileChooser.APPROVE_OPTION){
				setDotLocation(preferences, fc.getSelectedFile().toPath().toString());
			}
			else{
				System.exit(0);
			}
		}
		
		saveAs.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				console.saveText();
			}
		});
	}
	
	public static String getDotLocation(File f){
		try{
			BufferedReader reader = new BufferedReader(new FileReader(f));
			String prefs = "";
			String line = null;
			while ((line = reader.readLine()) != null){
				prefs += line;
			}
			reader.close();
			String parts[] = prefs.split("<Dot>");
			String dot = parts[1].split("</Dot>")[0];
			if (!dot.equals("")){
				return dot;
			}
		} 
		catch (FileNotFoundException e){
			e.printStackTrace();
		} 
		catch (IOException e){
			e.printStackTrace();
		}
		return null;
	}
	
	public static void setDotLocation(File f, String value){
		try{
			BufferedReader reader = new BufferedReader(new FileReader(f));
			String doc = "";
			String line = null;
			while ((line = reader.readLine()) != null){
				doc += line + "\n";
			}
			reader.close();
			String split[] = doc.split("<Dot>");
			String output = split[0] + "<Dot>" + value + "</Dot>" + doc.split("</Dot>")[1];
			BufferedWriter writer = new BufferedWriter(new FileWriter(f));
			writer.write(output);
			writer.close();
		} 
		catch (FileNotFoundException e){
			e.printStackTrace();
		} 
		catch (IOException e){
			e.printStackTrace();
		}
		
	}
	
}
